package zy.blue7.gui.netty.custom;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @Author: blue7
 * @Date: 2022/2/27 10:42
 * @Version 1.0
 * @Description
 */
public class Blue7MessageDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
        /**
         * 读取版本号
         */
        int version = in.readInt();

        /**
         * 读取 内容长度
         */
        int contentLength = in.readInt();

        /**
         *
         * 获取版本号
         */
        byte[] sessionByte = new byte[4];
        in.readBytes(sessionByte);
        String sessionId = new String(sessionByte);

        /**
         * 组装消息头
         */
        Blue7Header blue7Header = new Blue7Header(version, contentLength, sessionId);

        byte[] content = new byte[contentLength];
        in.readBytes(content);
        /**
         * 组装消息体
         */
        Blue7Message blue7Message = new Blue7Message(blue7Header, new String(content));

        list.add(blue7Message);
    }
}
